/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: AppTest.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: AppTest
*
*******************************************************************************/

/** HEADER FILES **/
#include "AppTest.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/


/** GLOBAL FUNCTION DECLARATION **/
void AppTest_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);


/** LOCAL DATA **/


/** GLOBAL DATA **/


/** LOCAL FUNCTION **/


/** GLOBAL FUNCTION **/

/*******************************************************************************
* -Name            AppTest_GetVersionInfo
* -Brief           Returns the version information of this module 
* -Details         None
* -Param[out]      versioninfo: Pointer to the version information of this module
* -Return          None          
*******************************************************************************/
void AppTest_GetVersionInfo(
    Std_VersionInfoType* versionInfo
)
{
    versionInfo->vendorID = (uint16)YSAR_VENDOR_ID;
    versionInfo->moduleID = (uint8)APPTEST_MODULE_ID;
    versionInfo->sw_major_version = (uint8)APPTEST_SW_MAJOR_VERSION;
    versionInfo->sw_minor_version = (uint8)APPTEST_SW_MINOR_VERSION;
    versionInfo->sw_patch_version = (uint8)APPTEST_SW_PATCH_VERSION;
}

/*******************************************************************************
* -Name            AppTest_MainFunction
* -Brief           Processes for testing
* -Details         None
* -Return          None
*******************************************************************************/
void AppTest_MainFunction(
    void
)
{
    static uint16 testLedTimer;
    static uint8 ledFlag;
    
    static uint8 testTimer;
    static uint16 otherCan1SignalB;
    static uint16 otherCan1SignalBOld;
    static uint8 otherCan1SignalC;
    static uint8 otherCan1SignalA[5];
    static uint8 otherCan1SignalCOld;
    static uint8 writingData[16] = {0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF};
    static uint8 readData[100];
    uint8 blockIndex;

    uint8 canNmState;
    uint8 canNmMode;

    CanNm_GetState(0u, (uint8*)&canNmState, (uint8*)&canNmMode);
    AppCom_SetSignal_YT_Can1_SignalC_Motorola((uint8*)&canNmState);

    if(testTimer >= 100u)
    {
        AppCom_GetSignal_Other_SignalA_Motorola((uint8*)&otherCan1SignalA);
        AppCom_GetSignal_Other_Can1_SignalB_Intel((uint8*)&otherCan1SignalB);
        AppCom_GetSignal_Other_Can1_SignalC((uint8*)&otherCan1SignalC);
        
        if(otherCan1SignalCOld != otherCan1SignalC)
        {
            if(0x01 == otherCan1SignalC)
            {
                Dem_SetEventStatus(AppTestConf_DemEventId_DTC_0x000001, DEM_EVENT_STATUS_PREPASSED);
            }
            else if(0x02 == otherCan1SignalC)
            {
                Dem_SetEventStatus(AppTestConf_DemEventId_DTC_0x000001, DEM_EVENT_STATUS_PREFAILED);
            }
            else if(0x03 == otherCan1SignalC)
            {
                Dem_SetEventStatus(AppTestConf_DemEventId_DTC_0x000002, DEM_EVENT_STATUS_PREPASSED);
            }
            else if(0x04 == otherCan1SignalC)
            {
                Dem_SetEventStatus(AppTestConf_DemEventId_DTC_0x000002, DEM_EVENT_STATUS_PREFAILED);
            }
            else
            {
                /* Do nothing */
            }
        }

        if(otherCan1SignalBOld != otherCan1SignalB)
        {
            CanNm_TriggerRequest(otherCan1SignalB);

            if(otherCan1SignalB == 100u)
            {
                CAN_SilentMode();
            }

            if(otherCan1SignalB == 200u)
            {
                CAN_NormalMode();
            }
#if 0
            if(otherCan1SignalB < 12)
            {
                NvM_WriteBlock(otherCan1SignalB, writingData);
            }
            if((100 <= otherCan1SignalB) && (112 > otherCan1SignalB))
            {
                NvM_ReadBlock((otherCan1SignalB - 100), (uint8*)&readData[8 * (otherCan1SignalB - 100)]);
            }
#endif
        }
        testTimer = 0u;
        
        otherCan1SignalCOld = otherCan1SignalC;
        otherCan1SignalBOld = otherCan1SignalB;
    }
    else
    {
        testTimer++;
    }

    if(1000u < testLedTimer)
    {
        testLedTimer = 0u;
        if(SET == ledFlag)
        {
            ledFlag = RESET;
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_15, GPIO_PIN_RESET);
        }
        else
        {
            ledFlag = SET;
            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_15, GPIO_PIN_SET);
        }
    }
    else
    {
        testLedTimer++; 
    }
}

